package com.pansoft.openplanet.activity;

import android.content.ClipboardManager;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.efounder.chat.zxing.qrcode.QRCode;
import com.efounder.constant.EnvironmentVariable;
import com.efounder.http.EFHttpRequest;
import com.pansoft.openplanet.R;
import com.utilcode.util.ToastUtils;

import static com.efounder.frame.utils.Constants.KEY_APKDOWN_URL;
import static com.efounder.frame.utils.Constants.KEY_SETTING_APPID;
import static com.efounder.frame.utils.Constants.KEY_SETTING_DEFAULT_SERVER_ADDRESS;

/**
 * 邀请码
 * Created by will on 18-3-23.
 */

@Deprecated
public class MyInviteCodeActivity extends TCBaseActivity implements View.OnClickListener {

    private String TAG = this.getClass().getSimpleName();
    private String inviteCode, userName;
    private TextView tvInviteCode, tvInviteLeft;
    private ImageView ivCode;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.talk_chain_activity_my_invite_code);
        tvInviteCode = (TextView) findViewById(R.id.tv_invite_code);
        TextView tvCopy = (TextView) findViewById(R.id.tv_copy);
        tvCopy.setOnClickListener(this);
        ImageView ivBack = (ImageView) findViewById(R.id.iv_back);
        ivBack.setOnClickListener(this);
        TextView tvTitle = (TextView) findViewById(R.id.tv_title);
        tvTitle.setText("邀请码");
        ivCode = (ImageView) findViewById(R.id.iv_bar_code);
        if (getIntent().getExtras() != null) {
            inviteCode = getIntent().getExtras().getString("inviteCode");
            userName = getIntent().getExtras().getString("userName");
        }
//        tvInviteLeft = (TextView) findViewById(R.id.tv_invite_left);
        initData();
    }

    private void initData() {
//        showLoading(R.string.im_inload_default_text);
//        HashMap<String, String> map = new HashMap<>();
//        map.put("access_token", EnvironmentVariable.getProperty("tc_access_token"));
//        TCRequestUtil.getOAuthRequest(TAG, "oauth/userInfo", map, new TCRequestUtil.TCRequestCallback() {
//            @Override
//            public void onSuccess(String response) {
//                try {
//                    JSONObject userObject = new JSONObject(response);
//                    if (userObject.has("userInfo")) {
//                        JSONObject user = userObject.getJSONObject("userInfo");
//                        inviteCode = user.getString("inviteCode");
//                        userName = user.getString("userName");
                        tvInviteCode.setText(inviteCode);
////                        HashMap<String, String> param = new HashMap<>();
////                        param.put("inviteCode", inviteCode);
////                        TCRequestUtil.getCommonRequest(TAG, "user/verifyInviteCodeAvailabilityCount", param, new TCRequestUtil.TCRequestCallback() {
////                            @Override
////                            public void onSuccess(String response) {
//                                dismissLoading();
////                                try {
////                                    JSONObject jsonObject = new JSONObject(response);
////                                    if (jsonObject.has("inviteCodeAvailabilityCount")){
////                                        int leftTimes = jsonObject.getInt("inviteCodeAvailabilityCount");
////                                        if (leftTimes >= 0) {
////                                            tvInviteLeft.setText("剩余邀请次数" + leftTimes + "次");
////                                        }
////                                    }
//
////                                }catch (JSONException e) {
////                                        e.printStackTrace();
////                                        ToastUtil.showToast(getApplicationContext(), "网络错误");
////                                    }
////                            }
//
////                            @Override
////                            public void onFail(String error) {
////                                ToastUtil.showToast(getApplicationContext(), "网络错误");
////                                dismissLoading();
////                            }
////                        });
//                    }
//                } catch (JSONException e) {
//                    e.printStackTrace();
//                    ToastUtil.showToast(getApplicationContext(), "网络错误");
//                    dismissLoading();
//                }
//            }
//
//            @Override
//            public void onFail(String error) {
//
//                dismissLoading();
//                if (error != null && error.equals("TokenExpireLogOut")) {
//                    showLogOutAlert();
//                    return;
//                }
//                ToastUtil.showToast(getApplicationContext(), "网络错误");
//            }
//        });
        //动态生成二维码
        String url;
        if (EnvironmentVariable.getProperty(KEY_APKDOWN_URL) != null && !EnvironmentVariable.getProperty(KEY_APKDOWN_URL).equals("")) {
            url = EnvironmentVariable.getProperty(KEY_APKDOWN_URL);
        } else {
//            url = "http://" + com.efounder.constant.EnvironmentVariable.getProperty(KEY_SETTING_DEFAULT_SERVER_ADDRESS)
//                    + "/ESTMobile/ESPMobile/" + com.efounder.constant.EnvironmentVariable.getProperty(KEY_SETTING_APPID)
//                    + ".apk";
            url = "https://" + EnvironmentVariable.getProperty(KEY_SETTING_DEFAULT_SERVER_ADDRESS)
                    + "/ospstore/OSPMobileLiveAPP/" + EnvironmentVariable.getProperty(KEY_SETTING_APPID)
                    + ".apk";
        }
//        Log.i(TAG, "自动生成的二维码下载路径:" + url);
        // https://mobile.osp.cloud/ospstore/OSPMobileLiveAPP/OSPMobileLiveApp.apk
        Bitmap bm = QRCode.setQRcode(url, 800, 800);
        if (bm != null) {
            ivCode.setImageBitmap(bm);
        }
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.tv_copy) {
            // 从API11开始android推荐使用android.content.ClipboardManager
            // 为了兼容低版本我们这里使用旧版的android.text.ClipboardManager，虽然提示deprecated，但不影响使用。
            ClipboardManager cm = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
            // 将文本内容放到系统剪贴板里。
            String text = getResources().getString(R.string.invite_intro);
            cm.setText(String.format(text,inviteCode, userName));
            ToastUtils.setBgColor(getResources().getColor(R.color.black));
            ToastUtils.setMsgColor(getResources().getColor(R.color.op_text_white_color));
            ToastUtils.showShort("邀请码已复制");
        } else if (id == R.id.iv_back) {
            finish();
        }
    }

//    private void showLogOutAlert() {
//        new AlertDialog.Builder(this)
//                .setTitle(R.string.common_text_hint)
//                .setMessage("用户登录过期，请重新登录")
//                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
//                    @Override
//                    public void onClick(DialogInterface dialogInterface, int i) {
//                        EventBus.getDefault().post(new LogoutEvent());
//                    }
//                })
//                .setCancelable(false)
//                .create().show();
//    }

    @Override
    protected void onStop() {
        super.onStop();
        EFHttpRequest.cancelRequest(TAG);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
